package com.tarena.lbs.content.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tarena.lbs.content.dao.mapper.CategoryMapper;
import com.tarena.lbs.content.dao.repository.CategoryRepository;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.po.ArticleCategoryPO;
import com.tarena.lbs.content.pojo.query.ArticleCategoryQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
@Slf4j
public class CategoryRepositoryImpl implements CategoryRepository {

    @Autowired
    CategoryMapper categoryMapper;


    @Override
    public List<ArticleCategoryPO> pageList(ArticleCategoryQuery query) {
        log.debug("查询文章分类列表");
        //封装查询条件
        QueryWrapper< ArticleCategoryPO> queryWrapper = buildQueryWrapper(query);
        List<ArticleCategoryPO> articleCategoryPOS = categoryMapper.selectList(queryWrapper);
        return articleCategoryPOS;

    }
    private QueryWrapper< ArticleCategoryPO> buildQueryWrapper(ArticleCategoryQuery query) {
        QueryWrapper< ArticleCategoryPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(query.getCategoryName())) {
            queryWrapper.eq("category_name", query.getCategoryName());
        }
        if (query.getCategoryStatus() != null) {
            queryWrapper.eq("category_status", query.getCategoryStatus());
        }
        return queryWrapper;
    }

    @Override
    public void insert(ArticleCategoryPO articleCategoryPO) {
        categoryMapper.insert(articleCategoryPO);
    }

    @Override
    public void update(ArticleCategoryPO articleCategoryPO) {
        categoryMapper.updateById(articleCategoryPO);
    }

    @Override
    public void delete(ArticleCategoryParam categoryParam) {
        categoryMapper.deleteById(categoryParam.getId());
    }


}
